/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */


package com.hlingsoft.kkh.action;

import com.hlingsoft.kkh.dao.*;
import com.hlingsoft.kkh.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * 评论接口
 *
 * @author Herbert
 */
@Controller
@RequestMapping("/comment")
public class CommentAction extends BaseAction {

    @Autowired
    private CommentDao commentDao;
    @Autowired
    private MerchantDao merchantDao;
    @Autowired
    private AdminApplicationDao applicationDao;
    @Autowired
    private UserDao userDao;

    private static final int rows = 999;

    /**
     * 商户评论
     * @param merchantId
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping(value = "/list.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> list(
            @RequestParam(value = "merchantId") long merchantId,
            @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<Comment> commentList = commentDao.getAllListByMerchantId(merchantId, pageNum * rows, rows);

            MerchantInfo merchantInfo=merchantDao.getMerchantById(merchantId);

            resultMap.put("commentList", commentList);
            resultMap.put("score", merchantInfo.getScore());
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }

    public String encodeStr(String str) {
        try {
            return new String(str.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

}
